package com.ktjy.market.controller;

import com.ktjy.market.pojo.AppCategory;
import com.ktjy.market.pojo.BackendUser;
import com.ktjy.market.pojo.DataDictionary;
import com.ktjy.market.pojo.DevUser;
import com.ktjy.market.service.AppCategoryService;
import com.ktjy.market.service.BackendUserService;
import com.ktjy.market.service.DataDictionaryService;
import com.ktjy.market.service.DevUserService;
import org.apache.log4j.Logger;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import utils.Constants;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.List;

@RequestMapping("/dev")
@Controller
public class DevUserController {
    private Logger logger=Logger.getLogger(BackendUserController.class);

    @Resource
    private DevUserService devUserService;
    @Resource
    private DataDictionaryService dataDictionaryService;
    @Resource
    private AppCategoryService appCategoryService;


    @RequestMapping("/toDevLogin")
    public String toDevLogin(){
        return "/devlogin";
    }
    @RequestMapping("/toMain")
    public String toMain(){
        return "/developer/main";
    }

    @RequestMapping("/toList")
    public String toList(Model model,HttpServletRequest request){
        HttpSession session = request.getSession();
        DevUser devUser = (DevUser)session.getAttribute(Constants.DEV_USER_SESSION);
        List<DataDictionary>dataDictionaryList=dataDictionaryService.UserListById(devUser.getId());
        List<DataDictionary>dataDictionaryFlatFormList=dataDictionaryService.UserListByFlatFormId(devUser.getId());
        List<AppCategory>appCategoryList=appCategoryService.category1(1L);
        model.addAttribute("statusList",dataDictionaryList);
        model.addAttribute("flatFormList",dataDictionaryFlatFormList);
        model.addAttribute("categoryLevel1List",appCategoryList);
        return "/developer/appinfolist";
    }

    @PostMapping("/login")
    public String login(@RequestParam("devCode")String dev,
                        @RequestParam("devPassword")String password,
                        HttpSession session){
        int i = devUserService.getUserCount(dev);
        if (i > 0) {
            //调用service方法，进行用户匹配
            DevUser devUser = devUserService.login(dev, password);
            if (null != devUser) {
                //将登录用户放入session
                session.setAttribute(Constants.DEV_USER_SESSION, devUser);
                //登录成功，重定向到/user/toMain接口
                return "redirect:/dev/toMain";
            } else {
                session.setAttribute("error","密码输入错误!");
            }
        } else {
            //用户不存在，登录失败
            session.setAttribute("error","用户名不存在!");
        }
        return "redirect:/dev/toDevLogin";
    }
}
